<!--
 * @Author: wangming
 * @Date: 2020-09-09 14:04:55
 * @LastEditors: wangming
 * @LastEditTime: 2021-12-10 16:09:57
 * @Description:添加类别按钮
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="780px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>国际化配置</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form
      class="pl30 pr30"
      :model="FormTypeData"
      inline
      ref="Form"
      label-width="120px"
    >
      <!-- <el-form-item
        label="国家地区"
        prop="region"
        :rules="[
          { required: true, message: '请选择国家地区', trigger: 'blur' },
          { validator, trigger: 'blur' },
        ]"
      >
        <el-select
          style="width: 500px"
          v-model="FormTypeData.region"
          filterable
          allow-create
          placeholder="请选择国家地区"
          @change="conutryChange"
        >
          <el-option
            v-for="i in langCode"
            :key="i.label"
            :label="i.label"
            :value="i.label"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item
        label="语言标识"
        prop="key"
        :rules="[
          { required: true, message: '请选择语言标识', trigger: 'blur' },
          { validator, trigger: 'blur' },
        ]"
      >
        <el-select
          style="width: 500px"
          v-model="FormTypeData.key"
          filterable
          placeholder="请选择语言标识"
          @change="conutryCodeChange"
        >
        <!-- allow-create -->
          <el-option
            v-for="i in canSelectedLang"
            :key="i.value"
            :label="i.label"
            :value="i.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit" :loading="loading"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { cloneDeep } from "lodash";
import { LANG_CODE } from "@/utils/enum";
export default {
  computed: {
    canSelectedLang() {
      return this.langCode.filter((item) => {
        return this.selectedLang.every((ele) => ele.key != item.value);
      });
    },
  },
  data() {
    return {
      loading: false,
      showVisible: false,
      FormTypeData: { region: "", key: "" },
      callback: () => {},
      validator: (rule, value, callback) => {
        callback();
      },

      langCode: LANG_CODE,
      selectedLang: [],
    };
  },

  methods: {
    // conutryChange(val) {
    //   const item = this.langCode.filter((item) => item.label === val)[0];
    //   if (item) this.FormTypeData.key = item.value;
    // },
    conutryCodeChange(val) {
      const item = this.canSelectedLang.filter((item) => item.value === val)[0];
      if (item) this.FormTypeData.region = item.label;
    },
    addForm(langList) {
      this.selectedLang = langList;
      this.FormTypeData = {};
      return this.showModel();
    },
    editForm(data) {
      this.FormTypeData = { data };
      return this.showModel();
    },
    showModel() {
      this.showVisible = true;
      this.loading = false;
      this.$nextTick(() => {
        this.$refs.Form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        await this.$refs.Form.validate();
        await this.callback(cloneDeep(this.FormTypeData));
        this.loading = true;
        this.showVisible = false;
      } catch (error) {}
    },
  },
};
</script>

<style lang="scss" scoped></style>
